// setTimeout(function () {
//   location.reload()
// }, 1000);

$(document).ready(function () {
  $.ajax({
    url: "http://jx.xuzhixiang.top/ap/api/cart-list.php",
    type: "get",
    data: {
      id: 69007,
    },
    success: function (res) {
      console.log(res);

      let cart = document.querySelector(".cartList");
      let str = "";
      for (var i = 0; i < res.data.length; i++) {
        str += `
             <li data-id="${res.data[i].pid}">
             <input type="checkbox" class="cks">
             <img src="${res.data[i].pimg}">
             <p class="spname">${res.data[i].pname}</p>
             <span class="minus">-</span>
             <input type="text" value="${res.data[i].pnum}" class="num">
             <span class="plus">+</span>
             <b class="perPrice">￥${res.data[i].pprice}</b>
             <strong class="perTotalPrice">￥${res.data[i].pprice * res.data[i].pnum}</strong>
             <input type="button" value="删除" class="delBtn" />
             <p class="yj">单价:</P>
             </li>
        `;
        cart.innerHTML = str;
      }

      //  减
      $(".minus").click(function () {
        let pid = $(this).parent().attr("data-id");
        let num = $(this).next().val();
        console.log(num);
        if (num == 1) {
          return false;
        }
        num--;
        $(this).next().val(num)
        $.ajax({
          url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
          type: "get",
          data: {
            uid: 69007,
            pid: pid,
            pnum: num,
          },
          success: function (res) {
            console.log(res);
          },
        })
        //更改总价格
        $(this).parent().find(".perTotalPrice").html("￥" + $(this).parent().find(".num").val() * parseFloat($(this).parent().find(".perPrice").text().substr(1)))
        getSum();
      })

      //  加
      $(".plus").click(function () {
        let pid = $(this).parent().attr("data-id");
        let num = $(this).prev().val();
        console.log(num);

        num++;
        $(this).prev().val(num)
        $.ajax({
          url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
          type: "get",
          data: {
            uid: 69007,
            pid: pid,
            pnum: num,
          },
          success: function (res) {
            console.log(res);
          },
        })
        $(this).parent().find(".perTotalPrice").html("￥" + $(this).parent().find(".num").val() * parseFloat($(this).parent().find(".perPrice").text().substr(1)))
        getSum();
      })

      // 删除
      $(".delBtn").click(function () {
        let pid = $(this).parent().attr("data-id");
        $(this).parent().remove();
        $.ajax({
          url: "http://jx.xuzhixiang.top/ap/api/cart-delete.php",
          type: "get",
          data: {
            uid: 69007,
            pid: pid,
          },
          success: function (res) {
            console.log(res);
          }
        })
        getSum()
      })

      //全选
      $("#all").change(function () {
        $(".cks,#all").prop("checked", $(this).prop("checked"));
        getSum();

      })

      $(".cks").change(function () {
        if ($(".cks:checked").length === $(".cks").length) {
          $("#all").prop("checked", true);
        } else {
          $("#all").prop("checked", false);
        }

      })

      //总计
      getSum();
      function getSum() {
        let money = 0;

        $(".perTotalPrice").each(function (i, ele) {
          console.log($(ele).text());
          money += parseFloat($(ele).text().substr(1))
        })
        $("#price").text(money.toFixed(2))
      }

    }
  })
})